package com.tulink.yichutu.app;

import com.alibaba.fastjson.JSONObject;
import com.mxixm.fastboot.weixin.annotation.*;
import com.mxixm.fastboot.weixin.module.credential.WxJsTicketManager;
import com.mxixm.fastboot.weixin.module.media.WxMediaManager;
import com.mxixm.fastboot.weixin.module.message.*;
import com.mxixm.fastboot.weixin.module.user.WxUser;
import com.mxixm.fastboot.weixin.module.web.WxRequest;
import com.mxixm.fastboot.weixin.service.WxApiService;
import com.mxixm.fastboot.weixin.service.WxExtendService;
import com.mxixm.fastboot.weixin.web.WxUserManager;
import com.tulink.yichutu.biz.*;
import com.tulink.yichutu.common.exception.BusinessException;
import com.tulink.yichutu.dto.CashCodeDto;
import com.tulink.yichutu.dto.CashCodeGeneratorParamsDto;
import com.tulink.yichutu.dto.GoodsDto;
import com.tulink.yichutu.dto.VipDateChangeParamsDto;
import com.tulink.yichutu.entity.WxVipCashCouponEntity;
import com.tulink.yichutu.entity.WxVipGoodsEntity;
import com.tulink.yichutu.entity.WxVipOrderEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 易出图公众号
 */
@Slf4j
@WxController
public class WxApp {


    private final static Integer exchangeVipMaxRetries = 5;
    private final static Integer generatorCashCodeMax = 100;

    @Autowired
    private WxApiService wxApiService;
    @Autowired
    private WxMediaManager wxMediaManager;
    @Autowired
    private WxMessageTemplate wxMessageTemplate;
    @Autowired
    private WxExtendService wxExtendService;
    @Autowired
    private WxJsTicketManager wxJsTicketManager;
    @Autowired
    private WxUserManager wxUserManager;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CashCouponBiz cashCouponBiz;
    @Autowired
    private UserCacheBiz userCacheBiz;
    @Autowired
    private OrderBiz orderBiz;
    @Autowired
    private VipDateBiz vipDateBiz;
    @Autowired
    private GoodsBiz goodsBiz;
    @Autowired
    private CmdBiz cmdBiz;

    /**
     * 左侧菜单
     */
    @WxButton(group = WxButton.Group.LEFT, main = true, name = "会员")
    public void left() {
    }

    /**
     * 定义微信菜单，并接受事件
     */
    @WxButton(type = WxButton.Type.CLICK,
            group = WxButton.Group.LEFT,
            order = WxButton.Order.FIRST,
            name = "我的")
    public WxMessage leftFirst(WxRequest wxRequest, WxUser wxUser) {
        String userId = userCacheBiz.getUserId(wxUser.getOpenId());
        if(userId == null) {
            log.info("点击“我的会员”，未曾登录过: {}", wxUser.getOpenId());
            return WxMessage.Text.builder().content("您还未登录过易出图！").build();
        }
        Date endTime = vipDateBiz.getVipEndTime(userId);
        if(!vipDateBiz.isVip(endTime)) {
            log.info("点击“我的会员”：不是会员或已到期，{}, {}", userId, wxUser.getOpenId());
            return WxMessage.Text.builder().content("您当前不是会员，或会员已到期").build();
        } else if(vipDateBiz.isLifetime(endTime)) {
            log.info("点击“我的会员”：终身会员，{}, {}", userId, wxUser.getOpenId());
            return WxMessage.Text.builder().content("您好，尊贵的终身会员！").build();
        } else {
            String endTimeDisplay = VipDateBiz.getEndTimeFormat().format(endTime);
            log.info("点击“我的会员”：有效期为：{}，{}, {}", endTimeDisplay, userId, wxUser.getOpenId());
            return WxMessage.Text.builder().content("您的会员到期时间为: " + endTimeDisplay).build();
        }
    }

    /**
     * 定义微信菜单，并接受事件
     */
    @WxButton(type = WxButton.Type.VIEW,
            group = WxButton.Group.LEFT,
            order = WxButton.Order.SECOND,
            url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxed37495dbe6659bf&redirect_uri=https%3A%2F%2Fwx.tuzhiqian.com%2Fpay%2Findex.html&response_type=code&scope=snsapi_base&state=&connect_redirect=1&connect_redirect=1#wechat_redirect",
            name = "充值")
    public void leftSecond() {
    }

    /**
     * 定义微信菜单，并接受事件
     */
    @WxButton(type = WxButton.Type.CLICK,
            group = WxButton.Group.LEFT,
            order = WxButton.Order.THIRD,
            name = "兑换")
    public WxMessage leftThird(WxRequest wxRequest, WxUser wxUser) {
        String openId = wxRequest.getOpenId();
        String userId = userCacheBiz.getUserId(openId);
        if (StringUtils.isBlank(userId)) {
            log.info("点击“兑换码”: 未曾登录过：{}", wxUser.getOpenId());
            return WxMessage.Text.builder().content("请先通过易出图扫码登录后再进行兑换").build();
        }
        String key = getExchangeKey(wxRequest.getOpenId());
        BoundValueOperations boundValueOperations = redisTemplate.boundValueOps(key);
        boundValueOperations.set(0);
        boundValueOperations.expire(1, TimeUnit.DAYS);
        log.info("点击“兑换码”: {}", wxUser.getOpenId());
        return WxMessage.Text.builder().content(
                "请切换到聊天模式后输入兑换码\r\n\r\n" +
                        "兑换码购买请联系QQ号：2182347271"
        ).build();
    }

    /**
     * 中间菜单
     */
    @WxButton(group = WxButton.Group.MIDDLE, main = true, name = "支持")
    public void middle() {
    }

    /**
     * 定义微信菜单，并接受事件
     */
    @WxButton(type = WxButton.Type.CLICK,
            group = WxButton.Group.MIDDLE,
            order = WxButton.Order.FIRST,
            name = "使用教程")
    public WxMessage middleFirst(WxRequest wxRequest) {
        log.info("点击“使用教程”: {}", wxRequest.getOpenId());
        return WxMessage.newsBuilder()
                .addItem(
                        "使用教程",
                        "易出图使用教程图文+视频版",
                        "https://tuzhiqian-stamp-1258154415.cos.ap-guangzhou.myqcloud.com/public/yichutu/logo.png",
                        "https://www.yuque.com/cadprint/doc/hzz66p")
                .build();
    }

    /**
     * 定义微信菜单，并接受事件
     */
    @WxButton(type = WxButton.Type.CLICK,
            group = WxButton.Group.MIDDLE,
            order = WxButton.Order.SECOND,
            name = "常见问题")
    public WxMessage middleSecond(WxRequest wxRequest) {
        log.info("点击“常见问题”: {}", wxRequest.getOpenId());
        return WxMessage.newsBuilder()
                .addItem(
                        "常见问题解决方案",
                        "持续更新的常见问题解决方案列表",
                        "https://tuzhiqian-stamp-1258154415.cos.ap-guangzhou.myqcloud.com/public/yichutu/logo.png",
                        "https://docs.qq.com/doc/DY2xud1N3Qm1GcVF2")
                .build();
    }

    /**
     * 定义微信菜单，并接受事件
     */
    @WxButton(type = WxButton.Type.CLICK,
            group = WxButton.Group.MIDDLE,
            order = WxButton.Order.THIRD,
            name = "QQ群")
    public WxMessage middleThird(WxRequest wxRequest) {
        log.info("点击“QQ群”: {}", wxRequest.getOpenId());
        return WxMessage.textBuilder()
                .content(
                        "一群:1030021644(已满)\r\n二群:722620963\r\n\r\n" +
                                "QQ群热心大神云集，可以咨询各类问题，您也可以直接在公众号留言，管理员定期回复哦！")
                .build();
    }

    /**
     * 定义微信菜单，并接受事件
     */
    @WxButton(type = WxButton.Type.CLICK,
            group = WxButton.Group.MIDDLE,
            order = WxButton.Order.FORTH,
            name = "客服")
    public WxMessage service(WxRequest wxRequest) {
        log.info("点击“客服”: {}", wxRequest.getOpenId());
        return WxMessage.textBuilder()
                .content("客服QQ号：2182347271")
                .build();
    }

    /**
     * 中间菜单
     */
    @WxButton(group = WxButton.Group.RIGHT, main = true, name = "我们")
    public void right() {
    }

    /**
     * 定义微信菜单，并接受事件
     */
    @WxButton(type = WxButton.Type.CLICK,
            group = WxButton.Group.RIGHT,
            order = WxButton.Order.FIRST,
            name = "易出图")
    public WxMessage rightFirst(WxRequest wxRequest) {
        log.info("点击“易出图”: {}", wxRequest.getOpenId());
        return WxMessage.newsBuilder()
                .addItem(
                        "易出图官网",
                        "在PC端打开体验更佳",
                        "https://tuzhiqian-stamp-1258154415.cos.ap-guangzhou.myqcloud.com/public/yichutu/logo.png",
                        "https://www.yichutu.com")
                .addItem(
                        "视频教程",
                        "通过视频教程认识易出图",
                        "http://mmbiz.qpic.cn/mmbiz_jpg/s7T68t6l7z3TRODa6Sia8y2viaIIt5R9NNNTZjV24eNk6GAebJNichpPfWaic6DxdBYvyWC71BNia47oQEFONc9lvAg/0?wx_fmt=jpeg",
                        "https://mp.weixin.qq.com/s/ioTfKl--EYlV4PTlINg9kQ")
                .build();
    }

    /**
     * 定义微信菜单，并接受事件
     */
    @WxButton(type = WxButton.Type.CLICK,
            group = WxButton.Group.RIGHT,
            order = WxButton.Order.SECOND,
            name = "图纸签")
    public WxMessage rightSecond(WxRequest wxRequest) {
        log.info("点击“图纸签”: {}", wxRequest.getOpenId());
        return WxMessage.newsBuilder()
                .addItem(
                        "图纸签官网",
                        "高效、稳定、可靠的批量签章工具，支持DWF、PDF格式CA签章（官网在PC端打开体验更佳）",
                        "https://tuzhiqian-stamp-1258154415.cos.ap-guangzhou.myqcloud.com/public/tuzhiqian/ad.png",
                        "https://www.tuzhiqian.com")
                .addItem(
                        "视频教程",
                        "通过视频教程认识图纸签",
                        "http://mmbiz.qpic.cn/mmbiz_jpg/s7T68t6l7z3TRODa6Sia8y2viaIIt5R9NNfqL5oQHnwaggQO07aIxiaVUhllXNQceWJkJnV3ywzn0aImibgSiaAicwqA/0?wx_fmt=jpeg",
                        "https://mp.weixin.qq.com/s/qpbFIwm1bREY1onon8Pl2A")
                .addItem(
                        "更多教程",
                        "DWF图纸盖章、PDF图纸盖章、旋转图纸批量盖章等详细使用教程",
                        "https://tuzhiqian-stamp-1258154415.cos.ap-guangzhou.myqcloud.com/public/tuzhiqian/banner1.png",
                        "https://www.tuzhiqian.com/help/pdf/")
                .build();
    }

    /**
     * 定义微信菜单，并接受事件
     */
    @WxButton(type = WxButton.Type.CLICK,
            group = WxButton.Group.RIGHT,
            order = WxButton.Order.THIRD,
            name = "联系我们")
    public WxMessage our(WxRequest wxRequest) {
        log.info("点击“联系我们”: {}", wxRequest.getOpenId());
        return WxMessage.textBuilder()
                .content("QQ号：2182347271")
                .build();
    }

    /**
     * 接收兑换码
     *
     * @param content
     * @return the result
     */
    @WxMessageMapping(type = WxMessage.Type.TEXT, wildcard = CmdBiz.cmdExchangeVip)
    @WxAsyncMessage
    public String exchangeVipCommand(WxRequest wxRequest, String content) {
        try {
            String openId = wxRequest.getOpenId();
            String userId = userCacheBiz.getUserId(openId);
            if (StringUtils.isBlank(userId)) {
                log.info("收到兑换码: 未曾登录过过，{}, {}", content, wxRequest.getOpenId());
                return "请先通过易出图扫码登录后再进行兑换";
            }
            String key = getExchangeKey(openId);
            BoundValueOperations boundValueOperations = redisTemplate.boundValueOps(key);
            Long count = getLongValue(boundValueOperations.get());
            if(count == null) {
                log.info("收到兑换码: 引导进入兑换流程，{}, {}", content, wxRequest.getOpenId());
                return "请通过菜单：会员->兑换，进入兑换流程";
            }
            count = boundValueOperations.increment();
            String code = content.trim().toUpperCase();
            WxVipCashCouponEntity cashCouponEntity = cashCouponBiz.getCashCoupon(code);
            if (cashCouponEntity == null) {
                if(count >= exchangeVipMaxRetries) {
                    log.info("收到兑换码: 连续{}次输入错误兑换码，禁止兑换24小时，{}, {}", exchangeVipMaxRetries, content, wxRequest.getOpenId());
                    return "连续" + exchangeVipMaxRetries + "次输入错误兑换码，请24小时后再试！(错误代码：4)";
                } else {
                    log.info("收到兑换码: 兑换码错误({}/{})，{}", count, exchangeVipMaxRetries, wxRequest.getOpenId());
                    return "兑换码错误(" + count + "/" + exchangeVipMaxRetries + ")，连续" + exchangeVipMaxRetries + "此错误将禁止兑换24小时，请确定后再输入";
                }
            }
            if (cashCouponEntity.getStatus() != 0) {
                if(count >= exchangeVipMaxRetries) {
                    log.info("收到兑换码: 连续{}次输入错误兑换码，禁止兑换24小时，{}, {}", exchangeVipMaxRetries, content, wxRequest.getOpenId());
                    return "连续" + exchangeVipMaxRetries + "次输入错误兑换码，请24小时后再试！(错误代码：5)";
                } else {
                    log.info("收到兑换码: 兑换码错误({}/{})，{}, {}", count, exchangeVipMaxRetries, content, wxRequest.getOpenId());
                    return "兑换码错误(" + count + "/" + exchangeVipMaxRetries + ")，连续" + exchangeVipMaxRetries + "此错误将禁止兑换24小时，请确定后再输入";
                }
            }
            redisTemplate.delete(key);
            String msg = orderBiz.deal(userId, openId, cashCouponEntity);
            //兑换成功，移除兑换码缓存
            cashCouponBiz.removeCache(code);
            return msg;
        } catch (BusinessException ex) {
            log.error("兑换失败，代码：-9：{}，{}", content, ex.getMessage());
            return "兑换失败，代码：-9，请加群反馈";
        } catch (Exception ex) {
            log.error("兑换出错，代码：-1, {}", content, ex);
            return "兑换失败，代码：-1，请加群反馈";
        }
    }

    /**
     * 查询命令列表
     *
     * @param content
     * @return the result
     */
    @WxMessageMapping(type = WxMessage.Type.TEXT, wildcard = CmdBiz.cmdQueryCommand)
    @WxAsyncMessage
    public String queryCommand(WxRequest wxRequest, String content) {
        if (!cmdBiz.isSupperOpenId(wxRequest.getOpenId())) {
            //非超级用户不反回任何内容
            return null;
        }
        try {
            return String.join("\r\n", cmdBiz.getSupperCommands());
        } catch (Exception ex) {
            log.error("exec " + CmdBiz.cmdQueryCommand + " error", ex);
            return "查询失败:" + ex.getMessage();
        }
    }

    /**
     * 查询商品列表
     *
     * @param content
     * @return the result
     */
    @WxMessageMapping(type = WxMessage.Type.TEXT, wildcard = CmdBiz.cmdQueryGoodsList)
    @WxAsyncMessage
    public String queryGoodsList(WxRequest wxRequest, String content) {
        if (!cmdBiz.isSupperOpenId(wxRequest.getOpenId())) {
            //非超级用户不反回任何内容
            return null;
        }
        try {
            return cmdBiz.addCommand(CmdBiz.cmdQueryGoodsList, null);
        } catch (Exception ex) {
            log.error("exec " + CmdBiz.cmdQueryGoodsList + " error", ex);
            return "查询失败:" + ex.getMessage();
        }
    }

    /**
     * 生成兑换码
     *
     * @param content
     * @return the result
     */
    @WxMessageMapping(type = WxMessage.Type.TEXT, wildcard = CmdBiz.cmdGeneratorCashCode)
    @WxAsyncMessage
    public String generatorCashCodeCommand(WxRequest wxRequest, String content) {
        if (!cmdBiz.isSupperOpenId(wxRequest.getOpenId())) {
            //非超级用户不反回任何内容
            return null;
        }
        try {
            CashCodeGeneratorParamsDto paramsDto = getCommandParams(CmdBiz.cmdGeneratorCashCode, content, CashCodeGeneratorParamsDto.class);
            if(paramsDto == null) {
                paramsDto = new CashCodeGeneratorParamsDto();
                paramsDto.setName("xxx设计院-20211006");
                paramsDto.setCount(100);
                paramsDto.setGoodsId("tN0jU4aH0bG0fD0d");
                return "命令参数格式错误\r\n实例:\r\n" + getCommandDemo(CmdBiz.cmdGeneratorCashCode, paramsDto);
            }
            if(!paramsDto.valid()) {
                return "命令参数错误";
            }
            if(paramsDto.getCount() > generatorCashCodeMax) {
                return "单次最多允许生成" + generatorCashCodeMax + "个兑换码";
            }
            WxVipGoodsEntity goodsEntity = goodsBiz.getById(paramsDto.getGoodsId());
            if(goodsEntity == null) {
                return "商品不存在";
            }
            if(cashCouponBiz.hasName(paramsDto.getName())) {
                return "名称已存在";
            }
            return cmdBiz.addCommand(CmdBiz.cmdGeneratorCashCode, paramsDto);
        } catch (Exception ex) {
            log.error("exec " + CmdBiz.cmdGeneratorCashCode + " error", ex);
            return "生成失败:" + ex.getMessage();
        }
    }

    /**
     * 查询兑换码
     *
     * @param content
     * @return the result
     */
    @WxMessageMapping(type = WxMessage.Type.TEXT, wildcard = CmdBiz.cmdQueryCashCodeByName)
    @WxAsyncMessage
    public String queryCashCodeByNameCommand(WxRequest wxRequest, String content) {
        if (!cmdBiz.isSupperOpenId(wxRequest.getOpenId())) {
            //非超级用户不反回任何内容
            return null;
        }
        try {
            String paramsDto = getCommandParams(CmdBiz.cmdQueryCashCodeByName, content, String.class);
            if(StringUtils.isBlank(paramsDto)) {
                paramsDto = "xxx设计院-20211006";
                return "命令参数格式错误\r\n实例:\r\n" + getCommandDemo(CmdBiz.cmdQueryCashCodeByName, paramsDto);
            }
            return cmdBiz.addCommand(CmdBiz.cmdQueryCashCodeByName, paramsDto);
        } catch (Exception ex) {
            log.error("exec " + CmdBiz.cmdQueryCashCodeByName + " error", ex);
            return "查询失败:" + ex.getMessage();
        }
    }

    /**
     * 查询兑换码
     *
     * @param content
     * @return the result
     */
    @WxMessageMapping(type = WxMessage.Type.TEXT, wildcard = CmdBiz.cmdQueryCashCodes)
    @WxAsyncMessage
    public String queryCashCodesCommand(WxRequest wxRequest, String content) {
        if (!cmdBiz.isSupperOpenId(wxRequest.getOpenId())) {
            //非超级用户不反回任何内容
            return null;
        }
        try {
            List<String> paramsDto = getCommandArrayParams(CmdBiz.cmdQueryCashCodes, content, String.class);
            if(paramsDto == null) {
                paramsDto = new ArrayList<>();
                paramsDto.add("CC-" + UUID.randomUUID());
                paramsDto.add("CC-" + UUID.randomUUID());
                paramsDto.add("CC-" + UUID.randomUUID());
                return "命令参数格式错误\r\n实例:\r\n" + getCommandDemo(CmdBiz.cmdQueryCashCodes, paramsDto);
            }
            if(paramsDto.size() < 1) {
                return "请传入需要查询的兑换码";
            }
            return cmdBiz.addCommand(CmdBiz.cmdQueryCashCodeByName, paramsDto);
        } catch (Exception ex) {
            log.error("exec " + CmdBiz.cmdQueryCashCodes + " error", ex);
            return "查询失败:" + ex.getMessage();
        }
    }

    /**
     * 删除兑换码
     *
     * @param content
     * @return the result
     */
    @WxMessageMapping(type = WxMessage.Type.TEXT, wildcard = CmdBiz.cmdDeleteCashCodeByName)
    @WxAsyncMessage
    public String deleteCashCodeByNameCommand(WxRequest wxRequest, String content) {
        if (!cmdBiz.isSupperOpenId(wxRequest.getOpenId())) {
            //非超级用户不反回任何内容
            return null;
        }
        try {
            String paramsDto = getCommandParams(CmdBiz.cmdDeleteCashCodeByName, content, String.class);
            if(StringUtils.isBlank(paramsDto)) {
                paramsDto = "xxx设计院-20211006";
                return "命令参数格式错误\r\n实例:\r\n" + getCommandDemo(CmdBiz.cmdDeleteCashCodeByName, paramsDto);
            }
            List<WxVipCashCouponEntity> cashCouponEntities = cashCouponBiz.getByName(paramsDto);
            if(CollectionUtils.isEmpty(cashCouponEntities)) {
                return "没有找到";
            }
            cashCouponBiz.delete(cashCouponEntities.stream().map(WxVipCashCouponEntity::getCode).toArray(String[]::new));
            return "删除成功";
        } catch (Exception ex) {
            log.error("exec " + CmdBiz.cmdDeleteCashCodeByName + " error", ex);
            return "删除失败:" + ex.getMessage();
        }
    }

    /**
     * 删除兑换码
     *
     * @param content
     * @return the result
     */
    @WxMessageMapping(type = WxMessage.Type.TEXT, wildcard = CmdBiz.cmdDeleteCashCodes)
    @WxAsyncMessage
    public String deleteCashCodesCommand(WxRequest wxRequest, String content) {
        if (!cmdBiz.isSupperOpenId(wxRequest.getOpenId())) {
            //非超级用户不反回任何内容
            return null;
        }
        try {
            List<String> paramsDto = getCommandArrayParams(CmdBiz.cmdDeleteCashCodes, content, String.class);
            if(paramsDto == null) {
                paramsDto = new ArrayList<>();
                paramsDto.add("CC-" + UUID.randomUUID());
                paramsDto.add("CC-" + UUID.randomUUID());
                paramsDto.add("CC-" + UUID.randomUUID());
                return "命令参数格式错误\r\n实例:\r\n" + getCommandDemo(CmdBiz.cmdDeleteCashCodes, paramsDto);
            }
            if(paramsDto.size() < 1) {
                return "请传入需要删除的兑换码";
            }
            if(paramsDto.size() > 500) {
                return "单次最多删除500个兑换码";
            }
            List<WxVipCashCouponEntity> cashCouponEntities = cashCouponBiz.getByCodes(paramsDto.toArray(new String[0]));
            if(CollectionUtils.isEmpty(cashCouponEntities)) {
                return "没有找到";
            }
            cashCouponBiz.delete(cashCouponEntities.stream().map(WxVipCashCouponEntity::getCode).toArray(String[]::new));
            return "删除成功";
        } catch (Exception ex) {
            log.error("exec " + CmdBiz.cmdDeleteCashCodes + " error", ex);
            return "删除失败:" + ex.getMessage();
        }
    }

    /**
     * 修改用户套餐
     *
     * @param content
     * @return the result
     */
    @WxMessageMapping(type = WxMessage.Type.TEXT, wildcard = CmdBiz.cmdChangeUserVipDate)
    @WxAsyncMessage
    public String changeUserVipDate(WxRequest wxRequest, String content) {
        if (!cmdBiz.isSupperOpenId(wxRequest.getOpenId())) {
            //非超级用户不反回任何内容
            return null;
        }
        try {
            VipDateChangeParamsDto paramsDto = getCommandParams(CmdBiz.cmdChangeUserVipDate, content, VipDateChangeParamsDto.class);
            if(paramsDto == null) {
                paramsDto = new VipDateChangeParamsDto();
                paramsDto.setBizOrderId("业务订单号");
                paramsDto.setGoodsId("商品Id");
                return "命令参数格式错误\r\n实例:\r\n" + getCommandDemo(CmdBiz.cmdChangeUserVipDate, paramsDto);
            }
            if(StringUtils.isBlank(paramsDto.getBizOrderId())) {
                return "业务订单号不能为空";
            }
            if(StringUtils.isBlank(paramsDto.getGoodsId())) {
                return "商品Id不能为空";
            }
            return orderBiz.changeVipDate(paramsDto.getBizOrderId(), paramsDto.getGoodsId());
        } catch (Exception ex) {
            log.error("exec " + CmdBiz.cmdChangeUserVipDate + " error", ex);
            return "修改失败:" + ex.getMessage();
        }
    }

    private String getCommandDemo(String command, Object data) {
        return command.substring(0, command.length() - 1) + JSONObject.toJSONString(data);
    }

    private <T> T getCommandParams(String command, String content, Class<T> tClass) {
        try {
            String params = content.substring(command.length() - 1);
            return JSONObject.parseObject(params, tClass);
        } catch (Exception ex) {
        }
        return null;
    }

    private <T> List<T> getCommandArrayParams(String command, String content, Class<T> tClass) {
        try {
            String params = content.substring(command.length() - 1);
            return JSONObject.parseArray(params, tClass);
        } catch (Exception ex) {
        }
        return null;
    }

    private String getExchangeKey(String openId) {
        return String.format("exchange:%s", openId);
    }

    private Long getLongValue(Object obj) {
        try {
            if (obj instanceof Long) {
                return (Long) obj;
            } else if (obj instanceof Number) {
                return Long.valueOf(((Number) obj).longValue());
            } else if (obj instanceof String) {
                return Long.valueOf((String) obj);
            } else if (obj != null) {
                return Long.valueOf(obj.toString());
            }
        } catch (Exception ex) {

        }
        return null;
    }
}
